package com.yzt.message.custom;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.Spannable;
import android.text.SpannableString;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.core.content.ContextCompat;
import androidx.fragment.app.FragmentActivity;

import com.yzt.redpack.RedPkgDetailActivity;
import com.yzt.redpack.dialog.GameRedOpenDialog;
import com.yzt.message.R;
import com.yzt.p_base.utils.ToastUtil;
import com.yzt.zhuju.network.entity.dynamic.RedPkgDetailBean;
import com.yzt.zhuju.network.entity.message.RedBean;
import com.yzt.zhuju.network.entity.message.RedPackageStatusInfo;
import com.yzt.zhuju.network.AresSubscriber;
import com.yzt.zhuju.network.biz.BizLayer;
import com.yzt.zhuju.utils.ActivityManagerUtils;

import java.util.List;

import io.rong.imkit.conversation.extension.component.emoticon.AndroidEmoji;
import io.rong.imkit.model.UiMessage;
import io.rong.imkit.widget.adapter.IViewProviderListener;
import io.rong.imlib.model.MessageContent;

/**
 * 红包的容器
 */
public class RXRedPkgMessageItemProvider extends CustomBaseMessageItemProvider<RXRedPkgMessageContent> {

    public RXRedPkgMessageItemProvider() {
        mConfig.showReadState = true;
        mConfig.showContentBubble = false;
    }

    @Override
    protected ViewHolder onCreateMessageContentViewHolder(ViewGroup parent, int i) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.nomi_rc_item_red_pkg_message, parent, false);
        return new ViewHolder(view.getContext(), view);
    }

    @Override
    protected void bindMessageContentViewHolder(io.rong.imkit.widget.adapter.ViewHolder viewHolder, io.rong.imkit.widget.adapter.ViewHolder viewHolder1, RXRedPkgMessageContent content, UiMessage data, int position, List<UiMessage> list, IViewProviderListener<UiMessage> iViewProviderListener) {
        ViewHolder holder = (ViewHolder) viewHolder;
        RedBean redBean = content.getMessageExtra();
        if (redBean != null) {
            holder.title.setText(redBean.getDes() == null || redBean.getDes().equals("") ? "大吉大利，恭喜发财" : redBean.getDes());
            getRedPkgStatus(redBean.getRedPackageId(), holder);
        }
    }

    @Override
    protected boolean onItemClick(io.rong.imkit.widget.adapter.ViewHolder viewHolder, RXRedPkgMessageContent content,
                                  UiMessage message, int position, List<UiMessage> list,
                                  IViewProviderListener<UiMessage> iViewProviderListener) {
        RedBean redBean1 = content.getMessageExtra();
        if (redBean1 != null) {
            BizLayer.getInstance().getMessageModule().getRedPkgStatus(redBean1.getRedPackageId(), new AresSubscriber<RedPackageStatusInfo>() {
                @Override
                protected void onSuccess(RedPackageStatusInfo redBean) {
                    //红包状态 [ 0：未领取，1：过期，2：已领 ]
                    if (redBean.getRedPackageStatus() == 0) {
                        // 红包封面
                        BizLayer.getInstance().getDynamicModule().getRedPacketInfo(redBean1.getRedPackageId(), new AresSubscriber<RedPkgDetailBean>() {
                            @Override
                            protected void onSuccess(RedPkgDetailBean redPkgInfo) {
                                FragmentActivity topActivity = ActivityManagerUtils.getInstance().getTopActivity();
                                Bundle bundle = new Bundle();
                                bundle.putParcelable("extra_data", redPkgInfo);
                                bundle.putString("extra_targetId", message.getTargetId());
                                GameRedOpenDialog.showDialog(topActivity, GameRedOpenDialog.class, bundle);
                            }
                        });
                    } else if (redBean.getRedPackageStatus() == 1) {
                        ToastUtil.showMessage("红包已过期");
                    } else {
                        FragmentActivity topActivity = ActivityManagerUtils.getInstance().getTopActivity();
                        Intent intent = new Intent(topActivity, RedPkgDetailActivity.class);
                        intent.putExtra("extra_id", redBean1.getRedPackageId());
                        topActivity.startActivity(intent);
                    }
                }
            });
        }
        return false;
    }

    /**
     * 获取页面状态
     */
    private void getRedPkgStatus(String redPackId, ViewHolder holder) {
        BizLayer.getInstance().getMessageModule().getRedPkgStatus(redPackId, new AresSubscriber<RedPackageStatusInfo>() {
            @Override
            protected void onSuccess(RedPackageStatusInfo redBean) {
                if (redBean.getRedPackageStatus() == 0) {
                    holder.content.setText("专属红包");
                    holder.ll_main_left.setBackground(ContextCompat.getDrawable(holder.ll_main_left.getContext(),
                            R.mipmap.icon_message_red_pkg_bg));
                } else if (redBean.getRedPackageStatus() == 1) {
                    holder.content.setText("已过期");
                    holder.ll_main_left.setBackground(ContextCompat.getDrawable(holder.ll_main_left.getContext(),
                            R.mipmap.icon_message_red_pkg_bg_time_out));
                } else {
                    holder.content.setText("已领取");
                    holder.ll_main_left.setBackground(ContextCompat.getDrawable(holder.ll_main_left.getContext(),
                            R.mipmap.icon_message_red_pkg_bg_time_out));
                }
            }
        });
    }

    @Override
    protected boolean isMessageViewType(MessageContent messageContent) {
        return messageContent instanceof RXRedPkgMessageContent && !messageContent.isDestruct();
    }

    @Override
    public Spannable getSummarySpannable(Context context, RXRedPkgMessageContent data) {
        if (data == null) {
            return null;
        }
        String content = data.getContent();
        if (content != null) {
            if (content.length() > 100) {
                content = content.substring(0, 100);
            }
            return new SpannableString(AndroidEmoji.ensure(content));
        }
        return null;
    }

    private static class ViewHolder extends io.rong.imkit.widget.adapter.ViewHolder {

        TextView title, content;

        LinearLayout ll_main_left;

        public ViewHolder(Context context, View view) {
            super(context, view);
            this.title = view.findViewById(R.id.tv_title);
            this.content = view.findViewById(R.id.tv_content);
            this.ll_main_left = view.findViewById(R.id.ll_main_left);
        }
    }
}